//
//  ButtonSlide.m
//  ButtonSlide
//
//  Created by Handymood on 11-5-21.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "ButtonSlide.h"


@implementation ButtonSlide

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
    }
    return self;
}

-(void)initWithImages:(NSArray *)imgArray 
            slideSize:(CGSize ) inSlideSize 
              imgSize:(CGSize ) inImgSize
{
    type = @"BUTTON_SLIDE";
    CGPoint centerP= CGPointMake(inSlideSize.width/2,inSlideSize.height/2);
    CGFloat borderW = abs((inSlideSize.width-inImgSize.width)/2);
     
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, inSlideSize.width, inSlideSize.height);
    
    leftButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
    [leftButton addButtonFace:@"LEFT_ARROW_NORMAL" normalImgSuffix:@"png" downImgPrefix:@"LEFT_ARROW_DOWN" downImgSuffix:@"png"];
    [leftButton setXY:(borderW-leftButton.frame.size.width)/2 
                        offy:centerP.y-leftButton.frame.size.height/2];
    [leftButton addTarget:self action:@selector(mouseDownProcLeft:) forControlEvents:UIControlEventTouchDown];
       
    
    rightButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
    [rightButton addButtonFace:@"RIGHT_ARROW_NORMAL" normalImgSuffix:@"png" downImgPrefix:@"RIGHT_ARROW_DOWN" downImgSuffix:@"png"];
    [rightButton setXY:centerP.x+inImgSize.width/2+(borderW-rightButton.frame.size.width)/2 
                 offy:centerP.y-rightButton.frame.size.height/2];
    [rightButton addTarget:self action:@selector(mouseDownProcRight:) forControlEvents:UIControlEventTouchDown];
    
    //scroll init
    imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((inSlideSize.width-inImgSize.width)/2,0,inImgSize.width,inImgSize.height)];
    
    if(imgArray)
    {
        imgScrollView.contentSize = CGSizeMake([imgArray count]*inImgSize.width,inImgSize.height);//

        for(int i = 0 ; i<[imgArray count];i++)//
        {
            NSString *tempFileName = [imgArray objectAtIndex:i];
            NSString *fileNamePrefix = [tempFileName  substringWithRange:NSMakeRange(0, [tempFileName  length] - 4)];
            NSString *fileNameSuffix = [tempFileName  substringWithRange:NSMakeRange([tempFileName length]-3,3)];
            
            NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileNamePrefix ofType:fileNameSuffix];
            NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
            
            UIImage *img = [[UIImage alloc] initWithData:imageData];   
            UIImageView *uiImageView =[[UIImageView alloc] initWithFrame:CGRectMake(i*inImgSize.width,0, inImgSize.width,inImgSize.height)];
            
            uiImageView.image = img;              

            [imgScrollView addSubview:uiImageView];
            
            [uiImageView release];
            [img release]; 
        }        
    }
    
    imgScrollView.delegate = self;
    [imgScrollView setPagingEnabled:YES];
    [imgScrollView setClipsToBounds:YES];
    [imgScrollView setShowsHorizontalScrollIndicator:NO];
    imgScrollView.userInteractionEnabled = NO;
   
    
    [self addSubview:imgScrollView];
    [self addSubview:leftButton];
    [self addSubview:rightButton];
}

-(void)setXY:(CGFloat) inx 
        offy:(CGFloat) iny
{
        self.frame = CGRectMake(inx, iny, self.frame.size.width, self.frame.size.height);
}

-(void) mouseDownProcLeft:(id)sender
{
    UIButton *btn = sender;
    
    if(btn && leftButton && btn == leftButton)
    {
        CGFloat index;
        CGFloat pageNum;
    
        index = floor(imgScrollView.contentOffset.x/imgScrollView.frame.size.width);
        pageNum = floor(imgScrollView.contentSize.width/imgScrollView.frame.size.width);
    
        if(index>0)
        {
            index--;
            CGRect frame = imgScrollView.frame;
            frame.origin.x = imgScrollView.frame.size.width*index;
            [imgScrollView scrollRectToVisible:frame animated:YES];
        }
    }
}

-(void) mouseDownProcRight:(id)sender
{
    UIButton *btn = sender;
    
    if(btn && rightButton && btn == rightButton)
    {
        CGFloat index;
        CGFloat pageNum;
    
        index = floor(imgScrollView.contentOffset.x/imgScrollView.frame.size.width);
        pageNum = floor(imgScrollView.contentSize.width/imgScrollView.frame.size.width);
    
        if(index<pageNum-1)
        {
            index++ ;
            CGRect frame = imgScrollView.frame;
            frame.origin.x = imgScrollView.frame.size.width*index;
            [imgScrollView scrollRectToVisible:frame animated:YES];
        }
    }
}

-(void)reset
{
    [leftButton removeTarget:self action:@selector(mouseDownProcLeft:) forControlEvents:UIControlEventTouchDown];
    [rightButton removeTarget:self action:@selector(mouseDownProcRight:) forControlEvents:UIControlEventTouchDown];    
   
    [leftButton setUserInteractionEnabled:NO];
    [rightButton setUserInteractionEnabled:NO];

    [leftButton removeFromSuperview];
    [rightButton removeFromSuperview];
    
    for(UIView *subview in [imgScrollView subviews])
    {
        [subview removeFromSuperview];
    }

    imgScrollView.delegate = Nil;
    [imgScrollView removeFromSuperview];
    [imgScrollView release];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
